//index.js
const App = getApp()
import { MineModel } from '../../../models/mine.js'
const ModelMine = new MineModel()
Page({
  data: {
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar: false,
    inviteCode: '',
    switch: '',
    imgList: [],
    urlName:'weapp/wx/'
  },
  // 查询微信信息
  getWxInfo() {
    const params = {}
    ModelMine.getBandWxInfo(params).then(res => {
      this.data.imgList = []
      if(res.data.wxImg) {
        this.data.imgList.push(res.data.wxImg)
      }
      this.setData({
        inviteCode: res.data.inviteCode,
        imgList: this.data.imgList,
        switch: res.data.switch
      })
    }).catch()
  },
  // 输入微信号
  bindKeyInput: function(e) {
    this.setData({
      inviteCode: e.detail.value
    })
  },
  //获取上传的图片信息
  ImgUpload: function (e) {
    this.data.imgList = e.detail
    this.setData({
      imgList: this.data.imgList
    })
  },
  // 开关
  switch1Change(e) {
    if(e.detail.value){
      this.setData({
        switch: 1
      })
    } else {
      this.setData({
        switch: 0
      })
    }
  },
  // 保存
  save() {
    if(!this.data.inviteCode) {
      App.WxService.showToast({
        title: '请输入微信号',
        icon: 'none'
      });
      return
    }
    if(this.data.imgList.length < 1) {
      App.WxService.showToast({
        title: '请上传微信二维码',
        icon: 'none'
      });
      return
    }
    const params = {
      inviteCode: this.data.inviteCode,
      wxImg: this.data.imgList[0],
      isOpen: this.data.switch
    }
    ModelMine.updateBandWxInfo(params).then(res => {
      // console.log('123', res)
      App.WxService.showToast({ title: '保存成功', icon: 'none' })
      wx.redirectTo({
        url: '/pages/account/index/index'
      })
      // this.getWxInfo()
    }).catch()
  },

  onLoad: function(options) {
    // Do some initialize when page load.
    // 查询微信信息
    this.getWxInfo()
  },
  onReady: function() {
    // Do something when page ready.
  },
  onShow: function() {
    // Do something when page show.
  },
  onHide: function() {
    // Do something when page hide.
  },
  onUnload: function() {
    // Do something when page close.
  },
  onPullDownRefresh: function() {
    // Do something when pull down.
  },
  onReachBottom: function() {
    // Do something when page reach bottom.
  },
  onShareAppMessage: function () {
    // return custom share data when user share.
  }
})